package com.yanggeng.demo.beginnerguidedemo.activity;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;

import com.yanggeng.demo.beginnerguidedemo.R;
import com.yanggeng.demo.beginnerguidedemo.activity.BaseActivity;
import com.yanggeng.demo.beginnerguidedemo.utils.FileUtils;
import com.yanggeng.demo.beginnerguidedemo.utils.SendMailUtils;
import com.yanggeng.demo.beginnerguidedemo.utils.SnackbarUtil;
import com.yanggeng.demo.beginnerguidedemo.utils.ToastUtil;
import com.yanggeng.demo.beginnerguidedemo.utils.ValidateUtils;

import java.util.ArrayList;
import java.util.List;

import static com.yanggeng.demo.beginnerguidedemo.R.id.et_email;

/**
 * Created by yanggeng on 2017/8/24.
 */

public class SendMailActivity extends BaseActivity {

    private EditText etAddress, etTopic, etContent;
    private static final int REQUEST_FILE = 102;
    private Context context;
    private Uri fileUri;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_send_mail);
        setTitle(true, "发送邮件", "发送");
        context = this;
        etAddress = (EditText) findViewById(et_email);
        etTopic = (EditText) findViewById(R.id.et_topic);
        etContent = (EditText) findViewById(R.id.et_content);
        findViewById(R.id.btn_select_file).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        if (v.getId() == R.id.btn_select_file) {
            selectFile();
        }
    }

    @Override
    protected void navigationOnclick(int paramId) {
        super.navigationOnclick(paramId);
        if (paramId == RIGHT_TEXT) {
            sendEmail();
        }
    }

    private void selectFile() {
        Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
        intent.setType("*/*");
        intent.addCategory(Intent.CATEGORY_OPENABLE);
        startActivityForResult(intent, REQUEST_FILE);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_FILE && resultCode == RESULT_OK) {
            fileUri = data.getData();
            ToastUtil.centerToast(context, "选择文件路径：" + fileUri.toString());
        }
    }

    /**
     * 通过JavaMail发送邮件
     */
    private void sendEmail() {
        final List<String> pathList = new ArrayList<>();
        String address = etAddress.getText().toString().trim();
        if (TextUtils.isEmpty(address)) {
            ToastUtil.centerToast(context, "请输入收件人邮箱");
            return;
        }
        if (!ValidateUtils.isEmail(address)) {
            ToastUtil.centerToast(context, "邮箱格式错误");
            return;
        }
        String topic = etTopic.getText().toString().trim();
        if (TextUtils.isEmpty(topic)) {
            ToastUtil.centerToast(context, "请输入主题");
            return;
        }

        String content = etContent.getText().toString().trim();

        if (fileUri != null) {
            String path = FileUtils.getPath2(context, fileUri);
            if (!TextUtils.isEmpty(path)) {
                Log.e("path", path);
                pathList.add(path);
            }
        }
        SnackbarUtil.shortSnackbar(main_view, "发送中");

        SendMailUtils.send(topic, content, address, pathList, new SendMailUtils.SendCallBack() {
            @Override
            public void sendBack(boolean isSuccess) {
                if (isSuccess) {
                    SnackbarUtil.shortSnackbar(main_view, "发送成功");
                } else {
                    SnackbarUtil.shortSnackbar(main_view, "发送失败");
                }
            }
        });


    }
}
